package com.flst.xlbibliotheque.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

import com.flst.xlbibliotheque.api.Author;
import com.flst.xlbibliotheque.api.Book;

@NamedQueries({
    @NamedQuery(name = "ALL_AUTHOR", query = "FROM Author ORDER BY id DESC"),
    @NamedQuery(name = "AUTHOR_BY_NAME", query = "FROM Author WHERE nom = :nom ORDER BY nom DESC")
    
})

@Entity(name = "Author")
public class AuthorImpl implements Author {

public static final String ALL_AUTHOR = "ALL_AUTHOR";

public static final String AUTHOR_BY_NAME = "AUTHOR_BY_NAME";

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;

private String nom;



@ManyToMany(fetch=FetchType.LAZY)
private List<BookImpl> booksByAuthor = new ArrayList<BookImpl>();



public AuthorImpl(){
    this.nom = "Auteur 1";

}

public AuthorImpl(String nom) {

	 this.nom =nom;
}
public AuthorImpl(Long i) {

	 this.id =i;
}

@Override
public Long getId() {
    return id;
}

@Override
public String getNom() {
    return nom;
}



@Override
public List<? extends Book> getBooks() {
	return booksByAuthor;
   //return bookByAuthors;
}

@Override
public void addBook(Book book) {
    if ( !getBooks().contains(book)) {
    	booksByAuthor.add((BookImpl) book);
    }
}

@Override
public void removeBook(Book book) {
	booksByAuthor.remove(book);
}



}

